package czk.concrent.juc.countDOwnLatch.practice;

import lombok.SneakyThrows;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @Author:ChenZhangKun
 * @Date: 2022/5/12 22:26
 */
public class HorseRun {
    public static void main(String[] args) {
        CountDownLatch latch=new CountDownLatch(1);
        new Thread(new Horse(latch,"A")).start();
        new Thread(new Horse(latch,"B")).start();
        new Thread(new Horse(latch,"C")).start();
        latch.countDown();
    }

}
class Horse implements Runnable {
    private CountDownLatch latch;
    private String horseName;

    public Horse(CountDownLatch latch, String horseName) {
        this.latch = latch;
        this.horseName = horseName;
    }

    @SneakyThrows
    @Override
    public void run() {
        // 死等
        latch.await();
        // 开始赛跑
        TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(5));
        Thread.currentThread().setName(horseName);
        System.out.println(Thread.currentThread().getName() + "到达终点了");
    }
}
